﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReplaceString
{
    /// <summary>
    /// Complexity O(n)
    /// </summary>
    public static class StringExtension
    {
        public static string StringReplace(this string input, string oldString, string newString)
        {
            string output = string.Empty;
            string temp = string.Empty;

            int i = 0;
            int j = 0;

            while (i < input.Length)
            {
                if (input[i] == oldString[j])
                {
                    temp += input[i++];
                    j++;

                    if (temp == oldString && j == oldString.Length)
                    {
                        output += newString;
                        temp = string.Empty;
                        j = 0;
                    }                    

                    continue;
                }

                if (temp != string.Empty)
                    output += temp;

                output += input[i++];
                temp = string.Empty;
                j = 0;

            }

            return output;
        }
    }
}
